﻿using System.Windows;
using System.Windows.Controls;
using CharacterCreation.Views.Contracts;
using System.ComponentModel;
using System.Linq;

namespace CharacterCreation.Views.ChildWindows {
    /// <summary>
    /// Interaction logic for NewCharacterWindow.xaml
    /// </summary>
    public partial class PopupWindow : Window, INotifyPropertyChanged {
        
        /// <summary>
        /// Gets or sets the IPopupView
        /// </summary>
        public BaseView[] IPopupViews {
            get { return _IPopupViews; }
            set {
                _IPopupViews = value.Select(p => {
                    p.IsPopup = true;
                    return p;
                }).ToArray();
                RaisePropertyChanged("IPopupViews");
            }
        }
        private BaseView[] _IPopupViews;
        
        /// <summary>
        /// Gets or sets the SelectedPopupView
        /// </summary>
        public BaseView SelectedPopupView {
            get { return _SelectedPopupView; }
            set {
                _SelectedPopupView = value;
                RaisePropertyChanged("SelectedPopupView");
                RaisePropertyChanged("HeaderText");
            }
        }
        private BaseView _SelectedPopupView;

        /// <summary>
        /// Teh visibility of the pages selection
        /// </summary>
        public Visibility PagesVisibility {
            get {
                return IPopupViews.Length > 1 ? Visibility.Visible : Visibility.Collapsed;
            }
        }
        
        /// <summary>
        /// Gets or sets the HeaderText
        /// </summary>
        public string HeaderText {
            get { return SelectedPopupView == null ? _HeaderText : SelectedPopupView.HeaderText; }
        }
        private string _HeaderText = "New character";

        /// <summary>
        /// Instantiates a popup window, pass the views which need to be shown on the left hand side of the popup.
        /// </summary>
        /// <param name="popupViews"></param>
        public PopupWindow(BaseView[] popupViews) {
            InitializeComponent();
            this.IPopupViews = popupViews;
            this.SelectedPopupView = IPopupViews[0];
            this.DataContext = this;

            lst.SelectionChanged += (s, e) => {
                lst.Focus();
            };
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e) {
            this.DialogResult = true;
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e) {
            this.DialogResult = false;
        }


        /// <summary>
        /// Raises the property changed event
        /// </summary>
        /// <param name="propertyName"></param>
        public void RaisePropertyChanged(string propertyName) {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// The property changed event
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
